package com.shushan.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.fingerprintidentify.FingerprintIdentify;
import com.android.fingerprintidentify.base.BaseFingerprint;
import com.shushan.base.BaseActivity;
import com.shushan.home.OpenSectVipActivity;
import com.shushan.invitecode.InviteCalendarActivity;
import com.shushan.pages.OpenVipSpendActivity;
import com.shushan.pages.bean.MaketingActivityBean;
import com.shushan.setting.ParentSettingActivity;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.ui.RoundProgressBar;
import com.shushan.ui.VerificationCodeView;


/**
 * @ClassName: DialogUtil
 * @Desciption: //对话框工具类
 * @author: zhangshihao
 * @date: 2018-03-28
 */
public class DialogUtil{

    private static final String TAG = "DialogUtil";

    private static Dialog mParentSetDialog;
    private static Dialog mDialog;
    private static Dialog mDownloadDialog;
    private static FingerprintIdentify mFingerprintIdentify;

    //弹出提示对话框
    public static void showHintDialog(Context context, int hintImgRes, String hintMsg) {
        if(context instanceof Activity && !((Activity)context).isFinishing()){
            mDialog = new Dialog(context, R.style.Dialog_Translucent_Background);
            mDialog.setContentView(R.layout.dialog_hint);
            ImageView iv = mDialog.findViewById(R.id.iv_hint_dialog);
            TextView tv = mDialog.findViewById(R.id.tv_hint_dialog);
            iv.setImageResource(hintImgRes);
            tv.setText(hintMsg);
            mDialog.setCancelable(true);
            mDialog.setCanceledOnTouchOutside(true);
            mDialog.show();
        }
    }

    public static void showVideoFinishDialog(Context context, View.OnClickListener listener){
        if(context instanceof Activity && !((Activity)context).isFinishing()){
            mDialog = new Dialog(context,R.style.Dialog_Translucent_Background);
            mDialog.setContentView(R.layout.dialog_video_finish);
            Button btnContinueStudy = mDialog.findViewById(R.id.btn_continue_study);
            Button btnStartExercise = mDialog.findViewById(R.id.btn_start_exercise);
            btnContinueStudy.setOnClickListener(listener);
            btnStartExercise.setOnClickListener(listener);
            mDialog.setCancelable(false);
            mDialog.show();
        }
    }

    public static void showSurpriseDialog(Context context,View.OnClickListener listener){
        if(context instanceof Activity && !((Activity)context).isFinishing()){
            mDialog = new Dialog(context,R.style.Dialog_Translucent_Background);
            mDialog.setContentView(R.layout.dialog_surprise);
            mDialog.setCanceledOnTouchOutside(true);
            Button btn = mDialog.findViewById(R.id.btn_get_reward);
            btn.setOnClickListener(listener);
            ImageView iv = mDialog.findViewById(R.id.iv_yellow_close);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();
                }
            });
            mDialog.show();
        }

    }

    public static void showSurpriseDialog(Context context,View.OnClickListener listener,String btnStr){
        if(context instanceof Activity && !((Activity)context).isFinishing()){
            mDialog = new Dialog(context,R.style.Dialog_Translucent_Background);
            mDialog.setContentView(R.layout.dialog_surprise);
            Button btn = mDialog.findViewById(R.id.btn_get_reward);
            btn.setText(btnStr);
            ImageView iv = mDialog.findViewById(R.id.iv_yellow_close);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();
                }
            });
            btn.setOnClickListener(listener);
            mDialog.show();
        }
    }

    public static void showVIPpurchaseDialog(Context context, View.OnClickListener listener, MaketingActivityBean activityBean){
        if(context instanceof Activity && !((Activity)context).isFinishing()){
            mDialog = new Dialog(context,R.style.Dialog_Translucent_Background);
            mDialog.setContentView(R.layout.dialog_vip_purchase);
            ImageView ivYellowClose = mDialog.findViewById(R.id.iv_yellow_close);
            ivYellowClose.setOnClickListener(listener);
            LinearLayout llOpenVip = mDialog.findViewById(R.id.ll_open_vip);
            llOpenVip.setOnClickListener(listener);
            LinearLayout llFreeUse = mDialog.findViewById(R.id.ll_free_use);
            llFreeUse.setOnClickListener(listener);
            TextView tvOriginPrize = mDialog.findViewById(R.id.tv_origin_prize);
            tvOriginPrize.setText(""+activityBean.cost_price);
            TextView tvDiscountPrize = mDialog.findViewById(R.id.tv_discount_prize);
            tvDiscountPrize.setText(""+activityBean.favorable_price);
            mDialog.setCancelable(false);
            mDialog.show();
        }

    }

    //忘记密码弹窗
    public static void showForgetPswDialog(Context context, View.OnClickListener listener){
        if(context instanceof Activity && !((Activity)context).isFinishing()){
            mDialog = new Dialog(context,R.style.Dialog_Translucent_Background);
            mDialog.setContentView(R.layout.dialog_forget_psw);
            ImageView ivCloseNobg = mDialog.findViewById(R.id.iv_close_no_bg);
            ivCloseNobg.setOnClickListener(listener);
            ImageView ivClientPhone = mDialog.findViewById(R.id.iv_client_phone_num);
            ivClientPhone.setOnClickListener(listener);
            mDialog.show();
        }
    }

    //输入家长密码弹窗
    public static VerificationCodeView showInputParentPswDialog(final Context context,
                                                                View.OnClickListener listener,
                                                                VerificationCodeView.OnCodeFinishListener codeListener,
                                                                final OnFingerSuccessListener onFingerSuccessListener){
        if(context instanceof Activity && !((Activity)context).isFinishing()){
            mParentSetDialog = new Dialog(context,R.style.Dialog_Translucent_Background);
            mParentSetDialog.setContentView(R.layout.dialog_parent_psw_input);
            mParentSetDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            VerificationCodeView veriCodeView = mParentSetDialog.findViewById(R.id.verification_code_view);
            veriCodeView.setOnCodeFinishListener(codeListener);
            Button btnFindBackPsw = mParentSetDialog.findViewById(R.id.btn_find_back_psw);
            btnFindBackPsw.setOnClickListener(listener);
            Button btnCancel = mParentSetDialog.findViewById(R.id.btn_cancel);
            btnCancel.setOnClickListener(listener);
            mParentSetDialog.setCancelable(false);

            if(mFingerprintIdentify == null){
                mFingerprintIdentify = new FingerprintIdentify(BabyApplication.getInstance(), new BaseFingerprint.FingerprintIdentifyExceptionListener() {
                    @Override
                    public void onCatchException(Throwable exception) {
                        Log.e(TAG,exception.getLocalizedMessage());
                    }
                });
            }

            ImageView parentImg = mParentSetDialog.findViewById(R.id.parent_psw_img);
            ImageView fingerIconImg = mParentSetDialog.findViewById(R.id.parent_finger_icon_img);
            final TextView hintText = mParentSetDialog.findViewById(R.id.parent_dialog_hint_tv);
            hintText.setVisibility(View.GONE);
            if(mFingerprintIdentify.isFingerprintEnable()
                    && mFingerprintIdentify.isHardwareEnable()
                    && mFingerprintIdentify.isRegisteredFingerprint()){

                parentImg.setVisibility(View.GONE);
                fingerIconImg.setVisibility(View.VISIBLE);

                ((TextView)mParentSetDialog.findViewById(R.id.parent_dialog_tv)).setText("请验证指纹或者家长密码");

                mFingerprintIdentify.startIdentify(5, new BaseFingerprint.FingerprintIdentifyListener() {
                    @Override
                    public void onSucceed() {
                        mParentSetDialog.dismiss();
                        if(onFingerSuccessListener != null){
                            onFingerSuccessListener.onFingerSuccess();
                        }
                    }

                    @Override
                    public void onNotMatch(int availableTimes) {
                        hintText.setVisibility(View.VISIBLE);
                        hintText.setText("指纹不匹配，还剩余" + availableTimes + "次机会");
                        Animation shake = AnimationUtils.loadAnimation(context, R.anim.shake);
                        hintText.startAnimation(shake);

                    }

                    @Override
                    public void onFailed(boolean isDeviceLocked) {
                        hintText.setText("指纹尝试过多，请使用家长密码");
                        Animation shake = AnimationUtils.loadAnimation(context, R.anim.shake);
                        hintText.startAnimation(shake);
                    }

                    @Override
                    public void onStartFailedByDeviceLocked() {
                        hintText.setText("指纹尝试过多，请使用家长密码");
                        Animation shake = AnimationUtils.loadAnimation(context, R.anim.shake);
                        hintText.startAnimation(shake);
                    }
                });
            }else {
                parentImg.setVisibility(View.VISIBLE);
                fingerIconImg.setVisibility(View.GONE);
                ((TextView)mParentSetDialog.findViewById(R.id.parent_dialog_tv)).setText("请输入家长密码");
            }

            mParentSetDialog.show();
            return veriCodeView;
        }else {
            return null;
        }

    }

    //输入家长密码退出登陆
    public static VerificationCodeView showParentLogoutDialog(Context context, View.OnClickListener listener, VerificationCodeView.OnCodeFinishListener codeListener){
        if(context instanceof Activity && !((Activity)context).isFinishing()){
            mParentSetDialog = new Dialog(context,R.style.Dialog_Translucent_Background);
            mParentSetDialog.setContentView(R.layout.dialog_parent_psw_logout);
            mParentSetDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            VerificationCodeView veriCodeView = mParentSetDialog.findViewById(R.id.verification_code_view);
            veriCodeView.setOnCodeFinishListener(codeListener);
            Button btnFindBackPsw = mParentSetDialog.findViewById(R.id.btn_find_back_psw);
            btnFindBackPsw.setOnClickListener(listener);
            Button btnCancel = mParentSetDialog.findViewById(R.id.btn_cancel);
            btnCancel.setOnClickListener(listener);
            mParentSetDialog.setCancelable(false);
            mParentSetDialog.show();
            return veriCodeView;
        }else {
            return null;
        }

    }

    //输入家长密码退出应用
    public static void showParentOutAppDialog(Context context, View.OnClickListener listener, VerificationCodeView.OnCodeFinishListener codeListener){
        if(context instanceof Activity && !((Activity)context).isFinishing()){
            mParentSetDialog = new Dialog(context,R.style.Dialog_Translucent_Background);
            mParentSetDialog.setContentView(R.layout.dialog_parent_psw_out_app);
            mParentSetDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            VerificationCodeView veriCodeView = mParentSetDialog.findViewById(R.id.verification_code_view);
            veriCodeView.setOnCodeFinishListener(codeListener);
            Button btnFindBackPsw = mParentSetDialog.findViewById(R.id.btn_find_back_psw);
            btnFindBackPsw.setOnClickListener(listener);
            Button btnCancel = mParentSetDialog.findViewById(R.id.btn_cancel);
            btnCancel.setOnClickListener(listener);
            mParentSetDialog.setCancelable(false);
            mParentSetDialog.show();
        }

    }

    public static void showFirstTimeLoginDialog(final BaseActivity context){
        if(context instanceof Activity && !context.isFinishing()){
            mDialog = new Dialog(context,R.style.Dialog_Translucent_Background);
            mDialog.setContentView(R.layout.dialog_first_time_login);

            ImageView ivGoto = mDialog.findViewById(R.id.iv_marketing_pic);
            ivGoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();

                    context.startActivityAllow(new Intent(context,InviteCalendarActivity.class));
                }
            });

            ImageView ivClose = mDialog.findViewById(R.id.iv_close_dialog);
            ivClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();
                }
            });
            mDialog.setCanceledOnTouchOutside(true);
            mDialog.show();
        }
    }


    public static void showNoVipDialog(final BaseActivity context, final int exerciseType){
        if(!context.isFinishing()){
            mDialog = new Dialog(context,R.style.Dialog_Translucent_Background);
            mDialog.setContentView(R.layout.no_all_vip_dialog);

            Button openAllBtn = mDialog.findViewById(R.id.no_vip_open_all_btn);
            openAllBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();

                    context.startActivity(OpenVipSpendActivity.class);
                }
            });

            Button openOneBtn = mDialog.findViewById(R.id.no_vip_one_btn);
            openOneBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();

                    Intent intent = new Intent(context, OpenSectVipActivity.class);
                    intent.putExtra("exerciseType",exerciseType);
                    context.startActivityAllow(intent);
                }
            });

            mDialog.setCanceledOnTouchOutside(true);
            mDialog.show();
        }
    }

    public static RoundProgressBar showDownloadProgressDialog(Context context){
        if(context instanceof Activity && !((Activity)context).isFinishing()){
            mDownloadDialog = new Dialog(context,R.style.Dialog_Translucent_Background);
            mDownloadDialog.setContentView(R.layout.dialog_download_progress);
            RoundProgressBar roundProgressBar = mDownloadDialog.findViewById(R.id.round_progress_bar);
            mDownloadDialog.setCancelable(false);
            mDownloadDialog.show();
            return roundProgressBar;
        }else {
            return null;
        }

    }

    public static void showRetroRuleDialog(Context context){
        if(context instanceof Activity && !((Activity)context).isFinishing()){
            mDialog = new Dialog(context,R.style.Dialog_Translucent_Background);
            mDialog.setContentView(R.layout.dialog_retro_rule);
            Button btnIknow = mDialog.findViewById(R.id.btn_i_know);
            btnIknow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismissDialog();
                }
            });
            mDialog.show();
        }
    }

    public static void dismissDownloadDialog(Context context){
        if(mDownloadDialog != null && mDownloadDialog.isShowing()
                && context instanceof Activity && !((Activity)context).isFinishing()){
            mDownloadDialog.dismiss();
            mDownloadDialog = null;
        }
    }

    public static void dismissParentSetDialog(){
        if (mParentSetDialog != null && mParentSetDialog.isShowing()) {
            mParentSetDialog.dismiss();
            mParentSetDialog = null;
        }

        if(mFingerprintIdentify != null){
            mFingerprintIdentify.cancelIdentify();
        }
    }

    public static void dismissDialog() {

        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

}
